package user;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

import util.DBConnection;

public class User {
	private DBConnection DB;
	private boolean isLogin=false;
	private String idName="";
	private String table="";
	private String id;
	private String name;
	private String nick_name;
	private String sex;
	private String telephone;
	private String email;
	private String password;
	private Date register_time;
	private Date last_login_time;
	public User(){
		DB=new DBConnection();
		DB.connect();
	}
	public boolean check(String id,String password){
		password=this.encriptThis(password);
		String sql="select * from " +table+" where "+idName+" like '"+id+"' and password like '"+password+"'";
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ResultSet rs=DB.executeQuery(sql);
		try{
		if(rs.next()){
			isLogin=true;
			this.id=rs.getString(idName);
			name=rs.getString("name");
			nick_name=rs.getString("nick_name");
			sex=rs.getString("sex");
			telephone=rs.getString("telephone");
			email=rs.getString("email");
			this.password=rs.getString("password");
			String registertime=rs.getString("register_time");
			String lastlogintime=rs.getString("last_login_time");
			register_time=dateFormat.parse(registertime);
			last_login_time=dateFormat.parse(lastlogintime);
			Date now=new Date();
			String Sql="update "+table+" set last_login_time='"+dateFormat.format(now)+"'";
			DB.executeUpdate(Sql);
			return true;
		}
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			DB.close(rs);
			DB.close();
		}
		return false;
	}
	protected void setIdName(String idName){
		this.idName=idName;
	}
	protected String getIdName(){
		return idName;
	}
	protected void setTable(String table){
		this.table=table;
	}
	protected String getTable(){
		return table;
	}
	public void setId(String id){
		this.id=id;
	}
	public String getId(){
		return id;
	}
	public void setPassword(String password){
		password=this.encriptThis(password);
		this.password=password;
	}
	public String getPassword(){
		return password;
	}
	public void setName(String name){
		this.name=name;
	}
	public String getName(){
		return name;
	}
	public void setSex(String sex){
		this.sex=sex;
	}
	public String getSex(){
		return sex;
	}
	public void setNickName(String nickname){
		this.nick_name=nickname;
	}
	public String getNickName(){
		return nick_name;
	}
	public void setTelephone(String telephone){
		this.telephone=telephone;
	}
	public String getTelephone(){
		return telephone;
	}
	public void setEmail(String email){
		this.email=email;
	}
	public String getEmail(){
		return email;
	}
	public void setRegisterTime(Date register_time){
		this.register_time=register_time;
	}
	public Date getRegisterTime(){
		return register_time;
	}
	public void setLastLoginTime(Date last_login_time){
		this.last_login_time=last_login_time;
	}
	public Date getLastLoginTime(){
		return last_login_time;
	}
	public void setIsLogin(boolean isLogin){
		this.isLogin=isLogin;
	}
	public boolean isLogin(){
		return isLogin;
	}
	public User findById(String id){
		String sql="select * from " +table+" where "+idName+" like '"+id+"'";
		ResultSet rs=DB.executeQuery(sql);
		User u=new User();
		try{
			if(rs.next()){
				u.setId(rs.getString(idName));
				u.setName(rs.getString("name"));
				u.setNickName(rs.getString("nick_name"));
				u.setEmail(rs.getString("email"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DB.close(rs);
			DB.close();
		}
		return u;
	}
	public boolean changeNickName(String nickname){
		String sql="update "+table+" set nick_name='"+nick_name+" where "+idName+"='"+id+"'";	
		boolean isChanged=DB.executeUpdate(sql);
		if(isChanged)
			setNickName(nickname);
		return isChanged;
	}
	public boolean changeTelephone(String telephone){
		String sql="update "+table+" set telephone='"+telephone+"' where "+idName+"='"+id+"'";
		boolean isChanged=DB.executeUpdate(sql);
		if(isChanged)
			setTelephone(telephone);
		return isChanged;
	}
	public boolean changeEmail(String email){
		String sql="update "+table+" set email='"+email+"' where "+idName+"='"+id+"'";
		boolean isChanged=DB.executeUpdate(sql);
		if(isChanged)
			setEmail(email);
		return isChanged;
	}
	public boolean changePassword(String password){
		password=this.encriptThis(password);
		String sql="update "+table+" set password='"+password+"' where "+idName+"='"+id+"'";
		boolean isChanged=DB.executeUpdate(sql);
		if(isChanged)
			setTelephone(password);
		return isChanged;
	}
	public boolean save(){
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql="insert into "+table+"("+idName+",password,name,nick_name,sex,telephone,email," +
				"register_time,last_login_time) values(?, ?, ?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement pstmt = DB.prepare(sql);
		try {
			pstmt.setString(1, id);
			pstmt.setString(2, password);
			pstmt.setString(3, name);
			pstmt.setString(4, nick_name);
			pstmt.setString(5, sex);
			pstmt.setString(6, telephone);
			pstmt.setString(7, email);
			pstmt.setString(8, dateFormat.format(register_time));
			pstmt.setString(9, dateFormat.format(last_login_time));
			pstmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} finally {
			DB.close(pstmt);
			DB.close();
		}
	}
	public String encriptThis(String password)
	{
		MessageDigest md=null;
  		byte[] bt=password.getBytes();
  		try {
   			md=MessageDigest.getInstance("md5");
   			byte[] digest = md.digest(bt);
         	password=byteToString(digest);
  		} catch (NoSuchAlgorithmException e) {
   			e.printStackTrace();
  		}
  		return password;
	}
	private static String byteToString(byte[] bts) {
		String des="";
   		String tmp=null;
  		for (int i=0;i<bts.length;i++) {
			//bts[i] & 0xFF <==> bts[i]+256 (if btd[i]<0)
        	tmp=(Integer.toHexString(bts[i] & 0xFF));
         	if (tmp.length()==1) {
            	des+="0";
        	}
        	des+=tmp;
      	}
      	return des;
	}
	public static void main(String[] args){
		User u=new User();
		System.out.println(u.encriptThis("123"));
	}


}
